package com.note.simpleadmin.module.shop.controller;

import com.note.simpleadmin.constant.ErrorCode;
import com.note.simpleadmin.dto.GoodsDto;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.shop.service.GoodsService;
import com.note.simpleadmin.module.shop.vo.GoodsVo;
import com.note.simpleadmin.vo.Msg;
import com.note.simpleadmin.vo.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/***
 *@ClaseName:GoodsController
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/2/3 0003 9:55
 */
@Controller
@RequestMapping(value = "shop/goods")
public class GoodsController{
    private final Logger logger = LoggerFactory.getLogger(GoodsController.class);
    @Autowired
    private GoodsService goodsService;
    @RequestMapping(value = "addGoods")
    public String addGoods()
    {
        return "shop/goods/addGoods";
    }
    @RequestMapping(value = "index")
    public String index() {
        return "shop/goods/goods";
    }
    @RequestMapping(value = "editGoods")
    public String editGoods() {
        return "shop/goods/editGoods";
    }
    @RequestMapping(value = "ajax_add")
    @ResponseBody
    public Msg ajax_add(@RequestBody  GoodsDto dto) {
        Msg msg=new Msg();
        msg.setErrorCode(ErrorCode.ERROR_OK);
        try {
            goodsService.save(dto);
            msg.setMsg("添加成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_update")
    @ResponseBody
    public Msg ajax_update(@RequestBody  GoodsDto dto) {
        Msg msg=new Msg();
        msg.setErrorCode(ErrorCode.ERROR_OK);
        try {
            goodsService.update(dto);
            msg.setMsg("更新成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_list")
    @ResponseBody
    public Msg ajax_list(String brandId,String classifyId,Integer status,String goodsName, Integer page,Integer limit) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            PageBean pageBean =goodsService.findByPage(brandId,classifyId,status,goodsName,page,limit);
            msg.setData(pageBean);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "ajax_detail")
    @ResponseBody
    public Msg ajax_detail(String id) {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            GoodsVo goodsVo=goodsService.getOne(id);
            msg.setData(goodsVo);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "ajax_del")
    @ResponseBody
    public Msg ajax_del(String id)
    {
        Msg msg=new Msg();
        try {
            goodsService.delete(id);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("删除成功");
        }catch (DataNotExistException e)
        {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
            msg.setMsg(e.getMessage());
        }catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_shelf")
    @ResponseBody
    public Msg ajax_shelf(String id,Integer status)
    {
        Msg msg=new Msg();
        try {
            goodsService.shelf(id,status);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("状态已更新");
        }catch (DataNotExistException e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
            msg.setMsg(e.getMessage());
        }catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
}